<?php
/**
 * 装修公司回复评价
 */
namespace Beerus\Modules\DecoCompany\Controllers;

use App\Http\Controllers\Controller;
use Beerus\Modules\DecoCompany\Actions\CancelFold;
use Beerus\Modules\DecoCompany\Actions\delEvaluation;
use Beerus\Modules\DecoCompany\Actions\FoldEvaluation;
use Beerus\Modules\DecoCompany\Actions\RecoverEvaluate;
use Doraemon\model\DecoCompanyEvaluation;
use Doraemon\model\Member;
use Doraemon\service\DecoCompanyEvaluationService;
use Doraemon\tools\ImgUrl;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Grid\Displayers\DropdownActions;
use Illuminate\Http\Request;
use Encore\Admin\Grid;
use App\Pockets\GridDataModel as pGridDataModel;
use Encore\Admin\Layout\Content;
use Encore\Admin\Widgets\Tab;
use Doraemon\service\Sysmsg;

class EvaluationReplyController extends Controller
{
    const DEFAULT_HAOHAOZHU_UID = 11941;
    const PAGE_SIZE = 20;

    public function index(Request $request, Content $content)
    {
        $page_size = $request->input("per_page", self::PAGE_SIZE);
        $page = $request->input("page", 1);

        $condition = [
            'type' => DecoCompanyEvaluation::TYPE_REPLY,
        ];
        if (!empty($request['to_user'])) {
            if (is_numeric($request['to_user'])) {
                $to_uid = $request['to_user'];
            } else {
                $to_uid = Member::get('uid', ['nick' => $request['to_user']]);
            }
            $condition['to_uid'] = $to_uid;
        }
        if (!empty($request['from_user'])) {
            if (is_numeric($request['from_user'])) {
                $from_uid = $request['from_user'];
            } else {
                $from_uid = Member::get('uid', ['nick' => $request['from_user']]);
            }
            $condition['from_uid'] = $from_uid;
        }
        if (!empty($request['id'])) {
            $condition['id'] = $request['id'];
        }
        $total = DecoCompanyEvaluation::count($condition);
        $condition['ORDER'] = ['created_at' => 'DESC'];
        $condition['LIMIT'] = [
            ($page - 1) * $page_size,
            $page_size
        ];
        $lists = DecoCompanyEvaluation::select('*', $condition) ?: [];
        self::formatList($lists);

        $gridModel = new pGridDataModel($lists, $total, [
            'perPage' => $page_size
        ]);
        $search_params = http_build_query($condition);
        $gridList = Admin::grid(
            $gridModel,
            function (Grid $grid) use ($gridModel, $search_params) {
                $grid->paginate($gridModel->getPerPage());
                $grid->disableExport();
                $grid->disableCreateButton();

                //$grid->disableActions();
                $grid->setActionClass(DropdownActions::class);

                $grid->filter(function (Grid\Filter $filter) {
                    $filter->disableIdFilter();

                    $filter->column(1 / 3, function ($filter) {
                        $filter->equal('id', '评价id');
                    });

                    $filter->column(1 / 3, function ($filter) {
                        $filter->equal('from_user', '评价人uid/昵称');
                    });
                    $filter->column(1 / 3, function ($filter) {
                        $filter->equal('to_user', '被评价人uid/昵称');
                    });
                    $filter->expand();
                });

                $grid->column('id', 'id');
                $grid->column('to_uid', '装修公司UID');
                $grid->column('to_nick', '装修公司昵称')->display(function() {
                    return '<a href="https://frieza.haohaozhu.me/admin/user/Detail/' . $this->to_uid . '" target="_blank">' . $this->to_nick . '</a>';
                })->width(100);
                $grid->column('from_uid', '用户UID');
                $grid->column('from_nick', '用户昵称')->display(function() {
                    return '<a href="https://frieza.haohaozhu.me/admin/user/Detail/' . $this->from_uid . '" target="_blank">' . $this->from_nick . '</a>';
                })->width(100);
                $grid->column('content', '评价内容')->width(200);
                $grid->column('images', '评价图片')->width(180)->display(function() {
                    $img = '';
                    if ($this->images) {
                        foreach ($this->images as $imageUrl) {
                            $img .= "<a href='{$imageUrl['pic_url']}' target='_blank'><img width='80' height='80' style='margin-bottom: 10px;padding-right: 10px;' src='{$imageUrl['thumb_pic_url']}'></a>";
                        }
                    }
                    return $img;
                });
                $grid->column('data_status', '状态')->display(function() {
                    return DecoCompanyEvaluation::DATA_STATUS_TXT[$this->data_status];
                });
                $grid->column('created_date', '回复时间')->width(100);
                $grid->actions(function (Grid\Displayers\Actions $actions) {
                    $actions->disableView();
                    $actions->disableEdit();
                    $actions->disableDelete();
                    if ($actions->row->data_status == DecoCompanyEvaluation::STATUS_DELETE) {
                        $actions->add(new RecoverEvaluate());
                    }
                    if ($actions->row->data_status == DecoCompanyEvaluation::STATUS_NORMAL){
                        $actions->add(new delEvaluation());
                    }

                });
            }
        );

        $tab = new Tab();
        $tab->add('装企回复管理', $gridList->render());
        $content->row($tab);
        return $content
            ->header('装企回复管理')
            ->description(' ')
            ->breadcrumb(['text' => '装企回复管理']);
    }

    public static function formatList(&$list) {
        $all_from_uid = array_column($list, 'from_uid');
        $all_to_uid = array_column($list, 'to_uid');
        $all_uid = array_merge($all_from_uid, $all_to_uid);
        $all_uid = array_values(array_unique($all_uid));
        if (empty($all_uid)) {
            return [];
        }
        // 查询这一批评论用户及装修公司的用户信息
        $member_info = Member::select(['uid', 'nick'], ['uid' => $all_uid]);
        $member_map = array_column($member_info, 'nick', 'uid');

        foreach ($list as &$item) {
            $item['from_nick'] = $member_map[$item['from_uid']] ?: '';
            $item['to_nick'] = $member_map[$item['to_uid']] ?: '';
            $item['images'] = self::getUrl($item['images']);
            $item['created_date'] = date('Y-m-d H:i:s', $item['created_at']);
        }
        unset($item);
    }

    public static function getUrl($images)
    {
        $data = [];
        if (!empty($images)) {
            $result = json_decode($images, true);
            $result = empty($result) ? [] : $result;

            foreach ($result as $item) {
                $data[] = [
                    'pic_id' => $item['pic_id'],
                    'pic_url' => ImgUrl::getUrl(ImgUrl::APPTYPE_APP, $item['pic_id'], ImgUrl::IMAGE_SIZE_SQ_PHONE),
                    'thumb_pic_url' => ImgUrl::getUrl(ImgUrl::APPTYPE_APP, $item['pic_id'], ImgUrl::IMAGE_SIZE_SQ_THUMB)
                ];
            }
        }

        return $data;
    }


}
